package com.wl.代码;

import com.entity.Comment;
import com.entity.User;
import com.jch.Dao.CommentDao;
import com.jch.Dao.MovieDao;
import com.jch.Dao.UserDao;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;

public class Child3 extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1428805005194968289L;

	private JLabel Qita;// 背景
	private JLabel Album;// 专辑背景
	// 显示一个小手
	private Cursor hand = new Cursor(Cursor.HAND_CURSOR);

	private JTextArea text1;// 文本
	private JScrollPane Gd1;// 滚动

	private JTextArea text2;// 文本
	private JScrollPane Gd2;// 滚动

	private JTextArea text3;// 文本
	private JScrollPane Gd3;// 滚动

	private JButton Back_to_home;// 返回主页
	private JButton Want_to_see_a_movie;// 想看电影
	private JButton Comment;// 发表评论

	private MovieDao movieDao=new MovieDao();

	public MovieDao getMovieDao() {
		return movieDao;
	}

	private Date date=new Date();

	SimpleDateFormat sdf=new SimpleDateFormat();

	private CommentDao commentDao=new CommentDao();

	private UserDao userDao=new UserDao();
	public Child3(Child3 child3, boolean b, User user) {
		// TODO Auto-generated constructor stub
		super(child3, b);
		// 必须获取窗口的面板来设置背景颜色
		getContentPane().setBackground(Color.WHITE);
		setTitle("电影购票系统");

		// 实例化文本框

		com.entity.Movie movie = movieDao.queryMovie("25");

		// 实例化文本框
		text1 = new JTextArea();
		text1.setLineWrap(true);
		text1.setFont(new Font("微软雅黑",Font.PLAIN,12));
		text1.setText(movie.getmDetail());
		// 在文本框上添加滚动条
		Gd1 = new JScrollPane(text1);
		// 设置矩大小
		Gd1.setBounds(460, 0, 220, 168);
		// 默认的设置是超过文本框才会显示滚动条，以下设置让滚动条一直显示
		Gd1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// 把滚动条添加到容器里面
		add(Gd1);

		// 实例化文本框
		text2 = new JTextArea();
		// 在文本框上添加滚动条
		Gd2 = new JScrollPane(text2);
		// 设置矩大小
		Gd2.setBounds(100, 200, 600, 100);
		// 默认的设置是超过文本框才会显示滚动条，以下设置让滚动条一直显示
		Gd2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// 把滚动条添加到容器里面
		add(Gd2);

		// 实例化文本框
		text3 = new JTextArea();
		text3.setLineWrap(true);


		List<com.entity.Comment> comments = commentDao.queryCommentDesc(movie.getMid());
		StringBuilder sb = new StringBuilder();
		for (com.entity.Comment comment : comments) {

			user.setUid(comment.getUid());
			User user1 = userDao.queryUser(comment.getUid());

			System.out.println(comment);
			sb.append(user1.getuName()+"\t"+comment.getComment()+"\t"+comment.getComtime()+"\r\n");
		}
		text3.setText(sb.toString());
		// 在文本框上添加滚动条
		Gd3 = new JScrollPane(text3);
		// 设置矩大小
		Gd3.setBounds(100, 340, 600, 120);
		// 默认的设置是超过文本框才会显示滚动条，以下设置让滚动条一直显示
		Gd3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		// 把滚动条添加到容器里面
		add(Gd3);

		Qita = new JLabel(new ImageIcon(Child3.class.getResource("背景-雪.jpg")));
		Qita.setBounds(0, 0, 800, 485);

		Album = new JLabel(new ImageIcon(Child3.class.getResource("攀登者-缩小.jpg")));
		Album.setBounds(120, 0, 220, 168);
		Qita.add(Album);

		Back_to_home = new JButton("返回主页");
		Back_to_home.setBounds(180, 174, 100, 20);
		Back_to_home.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		Back_to_home.setForeground(Color.black);// 设置字体颜色
		Back_to_home.setCursor(hand);// 放上去显示小手
		Back_to_home.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				dispose();
			}
		});
		Qita.add(Back_to_home);

		Want_to_see_a_movie = new JButton("想看电影");
		Want_to_see_a_movie.setBounds(540, 174, 100, 20);
		Want_to_see_a_movie.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		Want_to_see_a_movie.setForeground(Color.black);// 设置字体颜色
		Want_to_see_a_movie.setCursor(hand);// 放上去显示小手
		Want_to_see_a_movie.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				dispose();
				new Movie(user);

			}
		});
		Qita.add(Want_to_see_a_movie);

		sdf.applyPattern("yyyy-MM-dd hh:mm:ss");
		Comment = new JButton("发表评论");
		Comment.setBounds(600, 306, 100, 20);
		Comment.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		Comment.setForeground(Color.black);// 设置字体颜色
		Comment.setCursor(hand);// 放上去显示小手
		Comment.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if ("".equals(text2.getText())) {
					JOptionPane.showMessageDialog(Child3.this, "发表不能为空");
					return;
				}else {
					new Thread() {
						@Override
						public void run() {
							date = new Date();
							com.entity.Comment comment = new Comment();
							comment.setMid(movie.getMid());
							comment.setUid(user.getUid());
							comment.setComment(text2.getText());
							comment.setComtime(sdf.format(date));
							int i = commentDao.insertComment(comment);
							System.out.println(i);
							text3.setText(  text3.getText() + text2.getText() + "\t\t\t"
									+user.getuName()+ "\t" + sdf.format(date) + "\n\n");
							text2.setText(null);

						}
					}.start();
				}
			}
		});
		Qita.add(Comment);

		add(Qita);
		setSize(800, 485);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
	}

	public static void main(String[] args) {
//	new Child3(null, true);
	}

}
